package Tests;

import junit.framework.Assert;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.Test;

import Core.*;
import Core.Character;

public class CharacterLevelerImplTests
{
	@Test
	public void levelUpFailsIfRequirementsArentMet()
	{
		Mockery context = new Mockery();
		
		final Character character = context.mock(Character.class);
		final CharacterClass testClass = context.mock(CharacterClass.class);
		
		context.checking(new Expectations() {{
			exactly(1).of(character).getLevelsIn(testClass); will(returnValue(0));
			exactly(1).of(testClass).canLevel(character, 1); will(returnValue(false)); }});

		CharacterLevelerImpl leveller = new CharacterLevelerImpl(null);
		
		try
		{
			leveller.levelUp(character, testClass);

			Assert.fail("level up didn't throw an exception");
		}
		catch (RequirementsNotMetException ex)
		{
			Assert.assertTrue("true isn't true?", true);
		}
		
		context.assertIsSatisfied();
	}
	
	@Test
	public void levelUpSunnyDay() throws RequirementsNotMetException
	{
		Mockery context = new Mockery();
		
		final Character character = context.mock(Character.class);
		final CharacterClass testClass = context.mock(CharacterClass.class);
		final CharacterCareer career = context.mock(CharacterCareer.class);
		final SnapshotApplier snapshotApplier = context.mock(SnapshotApplier.class);
		final LevelSnapshot snapshot = new LevelSnapshot();
		
		context.checking(new Expectations()
		{{
			exactly(1).of(character).getCareer(); will(returnValue(career));
			exactly(1).of(career).levelUp(testClass);
			exactly(1).of(character).getLevelsIn(testClass); will(returnValue(0));
			exactly(1).of(testClass).canLevel(character, 1); will(returnValue(true));
			exactly(1).of(testClass).findSnapshotByLevel(1); will(returnValue(snapshot));
			exactly(1).of(snapshotApplier).applySnapshotTo(character, snapshot);
		}});
		
		CharacterLevelerImpl leveller = new CharacterLevelerImpl(snapshotApplier);
		
		leveller.levelUp(character, testClass);
		
		context.assertIsSatisfied();
	}
}
